<?php


namespace app\api\model;


use app\api\model\Theme as ThemeModel;

class Theme extends BaseModel
{
    protected $type = [
        'update_time'=>'datetime'
    ];
    protected $hidden
        = [
            'delete_time',
            'topic_img_id',
            'head_img_id',
//            'update_time',
        ];

    public function topicImg(): \think\model\relation\BelongsTo
    {
        //return $this->hasOne(Image::class,'id','topic_img_id');
        return $this->belongsTo(Image::class, 'topic_img_id', 'id');
    }

    public function headImg(): \think\model\relation\HasOne
    {
        return $this->hasOne(Image::class, 'id', 'head_img_id');
    }

    public function products(): \think\model\relation\BelongsToMany
    {
        return $this->belongsToMany(
            Product::class,
            'theme_product',
            'product_id',
            'theme_id'
        );
    }

    public static function getThemeWithProducts($id)
    {
        return self::with(['products', 'topicImg', 'headImg'])
                   ->find($id);
    }
}